package java_test.io;

import java.io.*;

public class Exercise1 {
    public static void main(String[] args) {
        copyFileWithStream("D:\\Download\\aDrive\\视频\\yt1s.com - 米津玄師  MVLemon.mp4", "C:\\Users\\lemon\\Desktop\\米津玄師  MVLemon.mp4");
        copyFileWithBuffered("D:\\Download\\aDrive\\视频\\yt1s.com - 米津玄師  MVLemon.mp4", "C:\\Users\\lemon\\Desktop\\MVLemon.mp4");

    }

    public static void copyFileWithStream(String scr, String dest) {
        FileInputStream fis = null;
        FileOutputStream fos = null;

        try {
            fis = new FileInputStream(new File(scr));
            fos = new FileOutputStream(new File(dest));

            byte[] bytes = new byte[1024];
            int len;
            while ((len = fis.read(bytes)) != -1) {
                fos.write(bytes, 0, len);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void copyFileWithBuffered(String scr, String dest) {
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;

        try {
            bis = new BufferedInputStream(new FileInputStream(new File(scr)));
            bos = new BufferedOutputStream(new FileOutputStream(new File(dest)));

            byte[] bytes = new byte[1024];
            int len;
            while ((len = bis.read(bytes)) != -1) {
                bos.write(bytes, 0, len);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (bis != null) {
                try {
                    bis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if (bos != null) {
                try {
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

